package xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import dbStruct.Column;
import dbStruct.Connector;
import dbStruct.Connectors;
import dbStruct.DatabaseStructure;
import dbStruct.Node;
import dbStruct.Nodes;
import dbStruct.Table;

public class XMLWriter {
	private Document 	xml;
	private Element		root;
	private Element		table;
	private Element		column;
	@SuppressWarnings("unused")
	private Element		key;
	private Element		relation;
	private Element		pkRelation;
	private Element		fkRelation;
	private DatabaseStructure dbStruct;
	private Nodes		nodes;
	private Connectors  connectors;
	@SuppressWarnings("unused")
	private File		file;
	
	public XMLWriter(DatabaseStructure dbStructure)
	{
		this.dbStruct=DatabaseStructure.getInstance();
		this.nodes=dbStruct.getNodes();
		this.connectors=dbStruct.getConnectors();
	}
	
	public void create() throws IOException
	{
		xml = new Document();
		root = new Element("TABLES");
		xml.setRootElement(root);
		Vector<Node> tables = nodes.getTables();
		for(int t=0;t<tables.size();t++)
		{
			//ELEMENTO TABLE
			table = new Element("TABLE");
			Table tableNode = (Table) tables.get(t);
			String tableName = tableNode.getName();
			table.setAttribute("NAME", tableName);
			System.out.println("  Creata tabella " + tableName);
			//ELEMENTI COLUMN mi servono tutti gli attributi associati al nodo
			Vector<Node> attributes = Connectors.getInstance().getColumns(tableName);
			for(int i=0;i<attributes.size();i++)
			{
				Column column = (Column) attributes.get(i); //prendo il nodo
				//Scrivo l'elemento COLUMN in XML
				this.column = new Element("COLUMN");
				this.column.setAttribute("NAME",column.getName());
				this.column.setAttribute("INDEX", String.valueOf(column.isIndex()));
				this.column.setAttribute("PK",String.valueOf(column.isPrimary()));
				this.column.setAttribute("TYPE",String.valueOf(column.getColumnType()));
				this.column.setAttribute("TYPE_NAME",column.getTypeName());
				this.column.setAttribute("POSITION",String.valueOf(column.getOrdinalPosition()));
				this.column.setAttribute("DIMENSION",String.valueOf(column.getDim()));
				table.addContent(this.column);
				System.out.println("    Creato campo " + column.getName());
			}
			root.addContent(this.table);
		}
		
		for(int t=0;t<tables.size();t++)
		{	//Per ogni tabella prelevo le relazioni
			Table pkTableNode  = (Table) tables.get(t);
			String pkTableName = pkTableNode.getName();
			
			Vector<Connector> conns = connectors.getRelations(pkTableName);
			for(int i=0;i<conns.size();i++)
			{
				Connector conn = conns.get(i);
				Table fkTableNode  = (Table) conn.getDestination();
				String fkTableName = fkTableNode.getName(); 
				
				this.relation = new Element("RELATION");
				this.relation.setAttribute("TYPE",conn.getCardinality().toString());
				this.relation.setAttribute("NAME",pkTableName + "." + fkTableName);
				this.pkRelation = new Element("PK_RELATION");
				this.pkRelation.setAttribute("TABLE", pkTableName);
				this.fkRelation = new Element("FK_RELATION");
				this.fkRelation.setAttribute("TABLE",fkTableName);
			this.relation.addContent(this.pkRelation);
			this.relation.addContent(this.fkRelation);
			System.out.println("  Creata relazione " + pkTableName + " <- " + fkTableName);
			root.addContent(this.relation);
				
			}
		}	
		//Stampo il file su video
		XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
		out.output(xml, System.out);
	}
	
	public void write(File file) throws IOException
	{
		XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
		out.output(xml, new FileOutputStream(file));
	}
}
